package tianwang.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tianwang.model.CAFile;
import tianwang.model.CALexical;
import tianwang.model.CASentence;
import tianwang.model.CASentenceFile;
import tianwang.model.CAStyle;
import tianwang.util.IbatisUtil;
import tianwang.util.TianDebugUtil;

import com.ibatis.sqlmap.client.SqlMapClient;


public class IBatisDao {
	private SqlMapClient sqlMap = null;
	
	public IBatisDao() throws IOException{
		if(sqlMap == null){
			sqlMap =  IbatisUtil.getCASqlMapClient();
		}
	}
	
	
	public Integer insertFile(CAFile model) throws SQLException{
		try {
			sqlMap.startTransaction();
			
			Object resultModel = sqlMap.insert("ca.insertFile", model);
			
			sqlMap.commitTransaction();
			return (Integer) resultModel;

		} finally {
			sqlMap.endTransaction();
		}
	}
	
	public Integer insertSentence(CASentence model) throws SQLException{
		try {
			sqlMap.startTransaction();
			
			Object resultModel = sqlMap.insert("ca.insertSentence", model);
			
			sqlMap.commitTransaction();
			return (Integer) resultModel;

		} finally {
			sqlMap.endTransaction();
		}
	}
	
	public void insertSentenceFile(CASentenceFile model) throws SQLException{
		try {
			sqlMap.startTransaction();
			
			Object resultModel = sqlMap.insert("ca.insertSentenceFile", model);
			
			sqlMap.commitTransaction();
			
		} finally {
			sqlMap.endTransaction();
		}
	}
	
	public void updateLexical(CALexical model) throws SQLException{
		try {
			sqlMap.startTransaction();
			
			sqlMap.insert("ca.updateLexical", model);
			
			sqlMap.commitTransaction();
			
		} finally {
			sqlMap.endTransaction();
		}
	}
	
	public void insertLexical(CALexical model) throws SQLException{
		try {
			sqlMap.startTransaction();
			
			sqlMap.insert("ca.insertLexical", model);
			
			sqlMap.commitTransaction();
			
		} finally {
			sqlMap.endTransaction();
		}
	}
	
	public void insertLexicalOrUpdate(CALexical model) throws SQLException{
		if(model.getId() == null){
			this.insertLexical(model);
		}else{
			this.updateLexical(model);
		}
	}
	
	/**
	 * 从 CAFile加载CALexical
	 * 
	 * 
	 * @param caFile
	 * @return
	 * @throws SQLException 
	 */
	public CALexical getCALexicalFromCAFile(CAFile caFile) throws SQLException{
		CALexical cal = this.getCALexicalFromFileId(caFile.getId());
		if(cal == null){
			cal = new CALexical();
		}
		return cal;
	}
	
	public CALexical getCALexicalFromFileId(Integer fileId) throws SQLException{
		try{
			sqlMap.startTransaction();
			
			CALexical cal = (CALexical) sqlMap.queryForObject("ca.getCALexicalFromFileId", fileId);
			
			sqlMap.commitTransaction();
			
			return cal;
		}finally{
			sqlMap.endTransaction();
		}
	}
	
	public List<CAFile> getFileList(Integer startNum,Integer len) throws SQLException{
		try{
			Map<String,Integer> para = new HashMap<String,Integer>();
			para.put("start", startNum);
			para.put("len", len);
			
			sqlMap.startTransaction();
			
			List list = sqlMap.queryForList("ca.getFileList", para);
			
			sqlMap.commitTransaction();
			
			return list;
		}finally{
			sqlMap.endTransaction();
		}
	}
	
	//---------------------------------------------
	
	/**
	 * 依据 CAFile加载CALexical
	 * 
	 * 如果CAFile的id存储在style表中，则返回该对象；
	 * 如果不在，则新建一个对象
	 * 
	 * @param caFile
	 * @return
	 * @throws SQLException 
	 */
	public CAStyle getCAStyleFromCAFile(CAFile caFile) throws SQLException{
		String sql = "select * from style where file_id=" + caFile.getId();
		
		List<Object> cas =  this.selectSql(sql);
		if(cas == null || cas.size() == 0){
			return new CAStyle();
		}
		
		if(cas.size() > 1){
			TianDebugUtil.error("caFile查询结果不唯一，id:" + caFile.getId());
		}
		return (CAStyle) cas.get(0);
	}
	
	/**
	 * 在Style表中插入一条记录
	 * 
	 * 这里为了方便拼接字符串，将每次插入和更新file_id字段
	 * 
	 * @param model
	 * @throws SQLException
	 */
	public void insertStyle(CAStyle style) throws SQLException{
		String sql = "insert into style ( " + 
				("`file_id`") + 
				(style.getSimpleSentence() == null ? "" : ",`simpleSentence`") + 
				(style.getCompoundSentence() == null ? "" : ",`compoundSentence`") + 
				(style.getComplexSentence() == null ? "" : ",`complexSentence`") + 
				(style.getShortSentence() == null ?  "" : ",`shortSentence`") + 
				(style.getLongSentence() == null ?  "" : ",`longSentence`") +
				(style.getSdSentenceLength() == null ?  "" : ",`sdSentenceLength`") + 
				(style.getAvgSentenceLength() == null ? "" : ",`avgSentenceLength`") + 
				" ) values ( " + 
				(style.getFile_id()) + 
				(style.getSimpleSentence() == null ? "" : "," + style.getSimpleSentence()) + 
				(style.getCompoundSentence() == null ? "" : "," + style.getCompoundSentence()) + 
				(style.getComplexSentence() == null ? "" : "," + style.getComplexSentence()) + 
				(style.getShortSentence() == null ?  "" : "," + style.getShortSentence()) + 
				(style.getLongSentence() == null ?  "" : "," + style.getLongSentence()) +
				(style.getSdSentenceLength() == null ?  "" : "," + style.getSdSentenceLength()) + 
				(style.getAvgSentenceLength() == null ? "" : "," + style.getAvgSentenceLength()) +
				")";
				
		 this.insertSql(sql);
	}
	
	/**
	 * 在Style表中更新一条记录
	 * 
	 * 这里为了方便拼接字符串，将每次插入和更新file_id字段
	 * 
	 * @param style
	 * @throws SQLException
	 */
	public void updateStyle(CAStyle style) throws SQLException{
		String sql = "update style set " + 
				("file_id = " + style.getFile_id()) + 
				(style.getSimpleSentence() == null ? "" : ",simpleSentence = " + style.getSimpleSentence()) + 
				(style.getCompoundSentence() == null ? "" : ",compoundSentence = " + style.getCompoundSentence()) + 
				(style.getComplexSentence() == null ? "" : ",complexSentence = " + style.getComplexSentence()) + 
				(style.getShortSentence() == null ?  "" : ",shortSentence = " + style.getShortSentence()) + 
				(style.getLongSentence() == null ?  "" : ",longSentence = " + style.getLongSentence()) +
				(style.getSdSentenceLength() == null ?  "" : ",sdSentenceLength = " + style.getSdSentenceLength()) + 
				(style.getAvgSentenceLength() == null ? "" : ",avgSentenceLength = " + style.getAvgSentenceLength()) +
				"where id = " + style.getId();
				
		 this.updateSql(sql);
	}
	
	/**
	 * 更新或插入Style表
	 * 
	 * @param model
	 * @throws SQLException
	 */
	public void insertStyleOrUpdate(CAStyle style) throws SQLException{
		if(style.getId() == null){
			this.insertStyle(style);
		}else{
			this.updateStyle(style);
		}
	}
	
	
	/**
	 * 直接执行拼接好的sql语句
	 * 
	 * 拼接的过程在dao层中完成
	 * 
	 * 由于ibatis无法识别系统是插入还是更新，所以只能是分成insert和update两个函数
	 * 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	protected Object insertSql(String sql) throws SQLException{
		try{
			sqlMap.startTransaction();
			
			Object obj = sqlMap.insert("ca.executeSql", sql);
			
			sqlMap.commitTransaction();
			return obj;
		}finally{
			sqlMap.endTransaction();
		}
	}
	protected Object updateSql(String sql) throws SQLException{
		try{
			sqlMap.startTransaction();
			
			Object obj = sqlMap.update("ca.executeSql", sql);
			
			sqlMap.commitTransaction();
			return obj;
		}finally{
			sqlMap.endTransaction();
		}
	}
	protected List<Object> selectSql(String sql) throws SQLException{
		try{
			sqlMap.startTransaction();
			
			List<Object> obj = sqlMap.queryForList("ca.executeSql", sql);
			
			sqlMap.commitTransaction();
			return obj;
		}finally{
			sqlMap.endTransaction();
		}
	}
	
	
}
